#!/bin/bash
###############################################################################
# File name:
#     atlas_con.sh
# 
# Description: 
#     You can access atlas modules in aes100 via console.
#
# $1 - Atlas module number
#
# The atlas modules are mapped in machine as following:
# |21(F1)|22(F2)|23(F3)|24(F4)|  ttyS9
# |17(E1)|18(E2)|19(E3)|20(E4)|  ttyS8
# |13(D1)|14(D2)|15(D3)|16(D4)|  ttyS7
# |09(C1)|10(C2)|11(C3)|12(C4)|  ttyS6
# |05(B1)|06(B2)|07(B3)|08(B4)|  ttyS5
# |01(A1)|02(A2)|03(A3)|04(A4)|  ttyS4
#
# 1. Open the console ttySx according to the module number mapped.
# 2. Run the script with the module number inputed. 
# Exp: ./atlas_con.sh 1  Open ttyS4 first and then access atlas module A1.
###############################################################################
set +x

gpio_tty_init=0xcf
gpio_reset_init=0x0f
tty_mod_1=0x00
tty_mod_2=0x10
tty_mod_3=0x20
tty_mod_4=0x30

# Initial the gpios as output
function gpio_init()
{
    #A1 A2 A3 A4
    lHRAT -i2c -w8 0 0x25 0x06 0x00
    
    #B1 B2 B3 B4
    lHRAT -i2c -w8 0 0x25 0x07 0x00
    
    #C1 C2 C3 C4
    lHRAT -i2c -w8 0 0x21 0x06 0x00
    
    #D1 D2 D3 D4
    lHRAT -i2c -w8 0 0x21 0x07 0x00
    
    #E1 E2 E3 E4
    lHRAT -i2c -w8 0 0x22 0x06 0x00
    
    #F1 F2 F3 F4
    lHRAT -i2c -w8 0 0x22 0x07 0x00
}

# Configure the PCA9535 register value
function pca9535_config()
{
    atlas_num=$1
    case $atlas_num in
    #A1
    1)
    tty=$tty_mod_1
    addr=0x25
    cmd=0x02 
    ;;
    #A2
    2)
    tty=$tty_mod_2
    addr=0x25
    cmd=0x02     
    ;;
    #A3
    3)
    tty=$tty_mod_3
    addr=0x25
    cmd=0x02    
    ;;
    #A4
    4)
    tty=$tty_mod_4
    addr=0x25
    cmd=0x02   
    ;;
    #B1
    5)
    tty=$tty_mod_1
    addr=0x25
    cmd=0x03
    ;;
    #B2
    6)
    tty=$tty_mod_2
    addr=0x25
    cmd=0x03
    ;;
    #B3
    7)
    tty=$tty_mod_3
    addr=0x25
    cmd=0x03
    ;;
    #B4
    8)
    tty=$tty_mod_4
    addr=0x25
    cmd=0x03
    ;;
    #C1
    9)
    tty=$tty_mod_1
    addr=0x21
    cmd=0x02
    ;;
    #C2
    10)
    tty=$tty_mod_2
    addr=0x21
    cmd=0x02
    ;;
    #C3
    11)
    tty=$tty_mod_3
    addr=0x21
    cmd=0x02
    ;;
    #C4
    12)
    tty=$tty_mod_4
    addr=0x21
    cmd=0x02
    ;;
    #D1
    13)
    tty=$tty_mod_1
    addr=0x21
    cmd=0x03
    ;;
    #D2
    14)
    tty=$tty_mod_2
    addr=0x21
    cmd=0x03
    ;;
    #D3
    15)
    tty=$tty_mod_3
    addr=0x21
    cmd=0x03
    ;;
    #D4
    16)
    tty=$tty_mod_4
    addr=0x21
    cmd=0x03
    ;;
    #E1
    17)
    tty=$tty_mod_1
    addr=0x22
    cmd=0x02 
    ;;
    #E2
    18)
    tty=$tty_mod_2
    addr=0x22
    cmd=0x02
    ;;
    #E3
    19)
    tty=$tty_mod_3
    addr=0x22
    cmd=0x02
    ;;
    #E4
    20)
    tty=$tty_mod_4
    addr=0x22
    cmd=0x02
    ;;
    #F1
    21)
    tty=$tty_mod_1
    addr=0x22
    cmd=0x03
    ;;
    #F2
    22)
    tty=$tty_mod_2
    addr=0x22
    cmd=0x03
    ;;
    #F3
    23)
    tty=$tty_mod_3
    addr=0x22
    cmd=0x03
    ;;
    #F4
    24)
    tty=$tty_mod_4
    addr=0x22
    cmd=0x03
    ;;
    *)
    exit 1
    ;;
    esac
}

# Select the console
function con_select()
{
    val_tmp=$(lHRAT -i2c -r8 0 $addr $cmd|awk -F: '{print $2}')
    #val_tmp=`lHRAT -i2c -r8 0 $addr $cmd`
    val=$((val_tmp&gpio_tty_init|tty|gpio_reset_init))
    echo "val=$val"
    lHRAT -i2c -w8 0 $addr $cmd $val
}

# Print the Atlas mapping
function print_map()
{
    echo "|21(F1)|22(F2)|23(F3)|24(F4)| ttyS9"
    echo "|17(E1)|18(E2)|19(E3)|20(E4)| ttyS8"
    echo "|13(D1)|14(D2)|15(D3)|16(D4)| ttyS7"
    echo "|09(C1)|10(C2)|11(C3)|12(C4)| ttyS6"
    echo "|05(B1)|06(B2)|07(B3)|08(B4)| ttyS5"
    echo "|01(A1)|02(A2)|03(A3)|04(A4)| ttyS4"
}

################################################################################
#main
if [ X$1 = X ];then
    print_map
    exit 1
elif [ $1 -lt 1 ] || [ $1 -gt 24 ];then
    echo "The atlas module number is wrong!"
    exit 1
fi

gpio_init
pca9535_config $1
con_select 

exit 0


